package server;

import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import ocsf.server.*;

/**
 * The Class SGIServerV1.
 * @author Tomer Elkayam
 */
public class SGIServerV1 extends ObservableServer
{
	//Final variables*************************************************
	/**
	 * The default port to listen on.
	 */
	final public static int DEFAULT_PORT = 5555;
	
	//Constructors ****************************************************
	  
  /**
   * Constructs an instance of the SGIServerV1 server.
   *
   * @param port The port number to connect on.
   */
	public SGIServerV1(int port) 
	{
		super(port);
	}
	
	  /**
	   * This method called each time a new client connection is
	   * accepted.
	   *
	   * @param client the connection connected to the client.
	   */
	  @Override
	  protected synchronized void clientConnected(ConnectionToClient client) 
	  {
	    setChanged();
	    notifyObservers(new ObMessage(CLIENT_CONNECTED,client));
	  }
	  
	  /**
	   * This method called each time a client disconnects.
	   *
	   * @param client the connection with the client.
	   */
	  @Override
	  protected synchronized void clientDisconnected(ConnectionToClient client) 
	  {
	    setChanged();
	    notifyObservers(new ObMessage(CLIENT_DISCONNECTED,client));
	  }
	  
	  /**
	   * This method is called each time an exception
	   * is raised in a client thread.
	   * This implementation simply closes the
	   * client connection, ignoring any exception.
	   *
	   * @param client the client that raised the exception.
	   * @param exception the exception raised.
	   */
	  @Override
	  protected synchronized void clientException(ConnectionToClient client,
			  												Throwable exception)
	  {
		clientDisconnected(client);
	    try
	    {
	      client.close();
	    }
	    catch (Exception e) {}
	  }
	  
	  /**
	   * This method is used to handle messages coming from the client.
	   * Observers are notified by receiving the transmitted message.
	   *
	   * @param message The message received from the client.
	   * @param client The connection to the client.
	   * @see ocsf.server.ObservableOriginatorServer
	   */
	  @Override
	  protected synchronized void handleMessageFromClient
	    (Object message, ConnectionToClient client)
	  {
	     setChanged();
	     notifyObservers(new ObMessage(message.toString(),client));
	  }
	  
	  
	  
	  
	  
}
